<!DOCTYPE html>
<html lang="zh_CN">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>fetch示例</title>
</head>

<body>
    <button id="btn0" onclick="main.getData0()">fetch it heima</button>
    <button id="btn1" onclick="main.getData1()">fetch post</button>
    <button id="btn2" onclick="main.fetchData()">axios post</button>
    <button id="btn3" onclick="main.fetchData1()">fetch minimal api</button>
    <script defer type="module">
        import * as axios from "./axios.js";
    </script>
    <script>
        class program {
            obj = {
                "Id": 12,
                "Name": "Extra",
                "Price": 12.4
            };
            static Main() {
                // document.getElementById("btn1").onclick();
                const arr = [1, 3, 4, 5, 2];
                document.writeln(...arr);
                const arr1 = [1, 2, 3, 4];
                const arr2 = [5, 6, 7];
                document.writeln(...arr1, ...arr2);
            }
            async getData0() {
                console.log("Hello world!");
                const request = await fetch("http://ajax-base-api-t.itheima.net/api/addbook", {
                    method: "POST",
                    headers: {
                        "Content-Type": "application/json"
                    },
                    body: JSON.stringify({
                        "bookname": "Js Learn",
                        "author": "Extra",
                        "publisher": "O'raily"
                    })
                });
                const json = await request.json();
                console.log(json);
            }
            async getData1() {
                const res = await fetch("http://localhost:5217/api/pizza/post", {
                    method: "post",
                    headers: {
                        "Content-Type": "application/json"
                    },
                    body: JSON.stringify(this.obj)
                });
                const json = await res.json();
                console.log(json);
            }
            async fetchData() {
                await axios.post("http://localhost:5217/api/pizza/post", { Id: 12, Name: "Hello", Price: 12.4 }).then(res => console.log(res));
            }
            async fetchData1() {
                const res = await fetch("http://localhost:5217/minapi");
                const json = await res.text();
                console.log(json);
            }
        }
        var main = new program();
        program.Main();
    </script>
</body>

</html>
